package top.drinyeit.weilong.presenter;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.TextView;

import java.util.Map;

import top.drinyeit.weilong.R;
import top.drinyeit.weilong.activity.ConfirmOrderActivity;
import top.drinyeit.weilong.activity.MainActivity;
import top.drinyeit.weilong.activity.MyOrderActivity;
import top.drinyeit.weilong.dialog.CenterDialog;
import top.drinyeit.weilong.dialog.PayDialog;
import top.drinyeit.weilong.model.OrderBean;
import top.drinyeit.weilong.retrofit.HttpResponse;
import top.drinyeit.weilong.retrofit.RetrofitMethod;
import top.drinyeit.weilong.retrofit.subscriber.CommonSubscriber;
import top.drinyeit.weilong.retrofit.subscriber.SubscriberListener;

import static top.drinyeit.weilong.app.Constant.COMMON_SUCCESS;

/**
 * Created by wu on 2018/1/13.
 */

public class ConfirmOrderPresenter {
    private ConfirmOrderActivity activity;

    public ConfirmOrderPresenter(ConfirmOrderActivity activity) {
        this.activity = activity;
    }

    //生成订单
    public void addOrder(Map<String, Object> map) {
        activity.showLoading();
        CommonSubscriber<HttpResponse<OrderBean>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<OrderBean>>() {
            @Override
            public void onError(String e, int code) {
                activity.dismissLoading();
                activity.showToastMsg(e);
            }

            @Override
            public void onNext(HttpResponse<OrderBean> o) {
                activity.dismissLoading();
                if (o.getCode() == COMMON_SUCCESS) {
                    final PayDialog payDialog = new PayDialog((Activity) activity, o.getData().getOrder_info().getParent_order_info().getCommodity_total_price(),
                            o.getData().getOrder_info().getParent_order_info().getId());
                    payDialog.show();
                    payDialog.order_price.setText("¥ " + o.getData().getOrder_info().getParent_order_info().getCommodity_total_price());
                    payDialog.setCancelable(false);
                    payDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                        @Override
                        public void onCancel(DialogInterface dialog) {
                            payDialog.dismiss();
                        }
                    });
                }else {
                    activity.showToastMsg(o.getMessage());
                }
            }
        });
        RetrofitMethod.getInstance().addOrder(subscriber, map);
    }

    public static void showPayDialog(View view, Double price, String order_id) {
        final PayDialog payDialog = new PayDialog(getActivityFromView(view), price, order_id);
        payDialog.show();
        payDialog.order_price.setText("¥" + price);
        payDialog.setCancelable(false);
        payDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                payDialog.dismiss();
            }
        });
    }

    /**
     * try get host activity from view.
     * views hosted on floating window like dialog and toast will sure return null.
     * @return host activity; or null if not available
     */
    //解决TintContextWrapper cannot be cast to android.app.Activity
    public static Activity getActivityFromView(View view) {
        Context context = view.getContext();
        while (context instanceof ContextWrapper) {
            if (context instanceof Activity) {
                return (Activity) context;
            }
            context = ((ContextWrapper) context).getBaseContext();
        }
        return null;
    }

}
